<?php
declare(strict_types=1);

namespace App;

use App\Enum\Code;
use Hyperf\Utils\Contracts\Arrayable;
use Hyperf\Utils\Contracts\Jsonable;
use Stringable;

class BaseResponse implements Stringable, Arrayable, Jsonable
{
    public function __construct(
        public readonly Code $code,
        public readonly string $message,
        public readonly mixed $data = null,
    )
    {
    }


    public function toArray(): array
    {
        return [
            'code' => $this->code->value,
            'message' => $this->message,
            'data' => $this->data,
            'timestamp' => time(),
        ];
    }

    public function __toString(): string
    {
        return json_encode($this->toArray(), JSON_UNESCAPED_UNICODE);
    }
}